package com.lm.scau.dyb.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.adapter.CharactorAdapter;
import com.lm.scau.dyb.bean.MovieRole;
import com.lm.scau.dyb.db.Dalx.RoleDalx;
import com.lm.scau.dyb.fragment.TitleFragment;
import com.lm.scau.dyb.inter.BasicUIEvent;
import com.lm.scau.dyb.inter.UICore;
import com.lm.scau.dyb.view.SideBar;

import java.util.ArrayList;
import java.util.List;


/**
 * Function:
 * auth：liming on 2016/8/29 10:49
 * mail：liming@wxchina.com
 */
public class RoleActivity extends BaseActivity implements SideBar.OnTouchingLetterChangedListener, BasicUIEvent {
    private static final int MSG_GET_ROLES = 10;
    private static final int MSG_GET_ROLE_SUCCESS = 11;

    private ListView roleList;
    private SideBar sideBar;
    private TitleFragment titleBar;
    private List<MovieRole> roles;
    private String type;
    private Handler handler;
    private CharactorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_charactor);
        initView();
        initHandler();
        initData();
        initAction();
    }

    private void initAction() {
        roleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String role = roles.get(position).getRo_name();
                Bundle bundle =new Bundle();
                bundle.putString("role",role);
                bundle.putString("type",type);
                startActivity(MoviesActivity.class,bundle);
            }
        });
    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_GET_ROLE_SUCCESS:
                        dealRoles();
                        break;
                    default:
                        break;
                }
            }
        };
    }

    private void dealRoles() {
        adapter = new CharactorAdapter(roles, this);
        roleList.setAdapter(adapter);
        adapter.updateAlphasLetters();
    }

    private void initData() {
        roles = new ArrayList<>();
        Intent intent = getIntent();
        type = intent.getExtras().getString("type", "-1");
        if ("-1".equals(type)) {
            showToast("此分类暂无数据...");
            return;
        }
        UICore.eventTask(this,this, MSG_GET_ROLES, null, null, true);
    }

    private void initView() {
        roleList = (ListView) findViewById(R.id.charactor_list_view);
        sideBar = (SideBar) findViewById(R.id.sidebar);
        titleBar = (TitleFragment) getSupportFragmentManager().findFragmentById(R.id.title_fragment);
        titleBar.initTitle(false, true, "主演");
        Button pw = (Button) findViewById(R.id.fast_position);
        sideBar.setTextView(pw);
        sideBar.setOnTouchingLetterChangedListener(this);
    }

    @Override
    public void onTouchingLetterChanged(String s) {
        // 从导航栏选择字母传出的值
        int position = adapter.getAlphasLetterPostion(s);
        if (position != -1) {
            if (roleList.getHeaderViewsCount() > 0) {// 防止ListView有标题栏，本例中没有。

            } else {
                roleList.setSelectionFromTop(position, 0);
                Log.v("onClick", "position------" + position);
            }
        }
    }

    @Override
    public void execute(int msg, Object obj) {
        switch (msg) {
            case MSG_GET_ROLES:
                getRolesFromLocal();
                break;
            default:
                break;
        }
    }

    private void getRolesFromLocal() {
        RoleDalx dalx = new RoleDalx(this);
        roles = dalx.getRoleBytype(type);
        Message msg = Message.obtain(handler, MSG_GET_ROLE_SUCCESS);
        msg.sendToTarget();
    }
}
